package com.oa.official.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.official.common.utils.LoginUserInfoHelper;
import com.oa.official.mapper.SysUserMapper;
import com.oa.official.service.SysUserService;
import com.oa.official.common.utils.MD5;
import com.oa.official.entity.system.SysUser;
import com.oa.official.response.Result;
import com.oa.official.vo.system.SysUserQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 用户信息分页查询
     * @param page2
     * @param limit
     * @param sysUserQueryVo
     * @return
     */
    @Override
    public Result list(Integer page2, Integer limit, SysUserQueryVo sysUserQueryVo) {
        Page<SysUser> page = new Page<>(page2,limit);
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasText(sysUserQueryVo.getKeyword()),SysUser::getUsername,sysUserQueryVo.getKeyword());
        //ge 大于等于
        lambdaQueryWrapper.ge(StringUtils.hasText(sysUserQueryVo.getCreateTimeBegin()),SysUser::getCreateTime,sysUserQueryVo.getCreateTimeBegin());
        //le 小于等于
        lambdaQueryWrapper.le(StringUtils.hasText(sysUserQueryVo.getCreateTimeEnd()),SysUser::getCreateTime,sysUserQueryVo.getCreateTimeEnd());
        Page<SysUser> sysUsers = sysUserMapper.selectPage(page,lambdaQueryWrapper);
        return Result.success(sysUsers);
    }

    /**
     *删除用户
     * @param userId
     * @return
     */
    @Override
    public Result deleteById(Long userId) {
        int row = sysUserMapper.deleteById(userId);
        return row > 0 ? Result.success() : Result.error("删除角色失败");
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @Override
    public Result save(SysUser sysUser) {
        String password = sysUser.getPassword();
        sysUser.setPassword(MD5.encrypt(password));
        int row  = sysUserMapper.insert(sysUser);
        return row > 0 ? Result.success() : Result.error("新增用户失败");
    }
    /**
     * 修该用户
     * @param sysUser
     * @return
     */
    @Override
    public Result update(SysUser sysUser) {
        int row =  sysUserMapper.updateById(sysUser);
        return row > 0 ? Result.success() : Result.error("修改用户失败");
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public Result findByUserId(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        return Result.success(sysUser);
    }

    /**
     * 跟新用户状态，0 正常   1:停用
     * @param userId
     * @param status
     * @return
     */
    @Override
    public Result updateStatus(Long userId, Integer status) {
        SysUser sysUser = sysUserMapper.selectById(userId);
        sysUser.setStatus(status); //1:正常   0:停用
        sysUserMapper.updateById(sysUser);
        return Result.success();
    }

    /**
     * 获取当前登录用户的信息
     * @return
     */
    @Override
    public Result getCurrentUser() {
        SysUser sysUser = sysUserMapper.selectById(LoginUserInfoHelper.getUserId());
        return Result.success(sysUser);
    }

}